package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.pudao.PuDaoTxyComprehensiveRiskLabelRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 朴道-腾讯云综合风险标签
 *
 * @author guoqiang
 */

public class PuDaoTxyComprehensiveRiskLabelParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 朴道-腾讯云综合风险标签
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        PuDaoTxyComprehensiveRiskLabelRecord obj = (PuDaoTxyComprehensiveRiskLabelRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful);
        sb.append(formatValue(obj.getName())).append(CRT001);
        sb.append(formatValue(obj.getCode())).append(CRT001);
        sb.append(formatValue(obj.getMsg())).append(CRT001);
        sb.append(formatValue(obj.getSeqNo())).append(CRT001);
        sb.append(formatValue(obj.getRequestId())).append(CRT001);
        sb.append(formatValue(obj.getMessage())).append(CRT001);
        sb.append(formatValue(obj.getPostTime())).append(CRT001);
        sb.append(formatValue(obj.getCodeDesc())).append(CRT001);
        sb.append(formatValue(obj.getFound())).append(CRT001);
        sb.append(formatValue(obj.getResponseCode())).append(CRT001);
        sb.append(CRT001);
        JSONArray pois = obj.getMultipleLoansInfo();
        if(pois.isEmpty()){
            sb.append(CRT001);
            sb.append(CRT001);
            key.set(getKey());
            val.set((sb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
            return true;
        }
        StringBuilder TxSb = new StringBuilder(500);
        for(Object h: pois){
            TxSb.delete(0, TxSb.length());
            JSONObject dataJson = JSONObject.parseObject(JSONObject.toJSONString(h));
            TxSb.append(formatValue(dataJson.getString("RiskCode"))).append(CRT001);
            TxSb.append(formatValue(dataJson.getString("RiskCodeValue"))).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + TxSb).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_pu_dao_txy_comprehensive_risk_label_inc";
    }

    @Override
    public String getType() {
        return RecordType.PD_TXY_COMPREHENSIVE_RISK_LABEL_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "朴道-腾讯云综合风险标签";
    }
}
